package com.zhongke.cotrun.model.validator;

import com.zhongke.cotrun.model.exception.RRException;
import lombok.Data;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

@Data
public class UserValidator {
    private static Validator validator;
    public  interface InSet{};//新增分组
    public  interface UpDate{};//跟新分组
    public  interface Persion{};//个人组
    static {
        validator = Validation.buildDefaultValidatorFactory().getValidator();
    }



    /**
     * 校验对象
     *
     * @param object 待校验对象
     * @param groups 待校验的组
     * @throws RRException 校验不通过，则报RRException异常
     */
    public static void validateEntity(Object object, Class<?>... groups)
            throws RRException {
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
        if (!constraintViolations.isEmpty()) {
            ConstraintViolation<Object> constraint = (ConstraintViolation<Object>) constraintViolations.iterator().next();
            throw new RRException(constraint.getMessage());
        }
    }
}
